package server.main.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.main.config.BeanInsert.Beans.PermissionRefresh;
import server.main.config.cache.MybatisPlusRedisCache;
import server.main.pojo.Role;
import server.main.service.RolePermissionService;
import server.main.service.RoleService;

//import javax.xml.ws.Action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther MingHsZzz
 * @Date 2022/10/4  15:51
 * @project main
 * @description
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    PermissionRefresh permissionRefresh;

    @Autowired
    RoleService roleService;

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    MybatisPlusRedisCache mybatisPlusRedisCache;
    /**
     * 以分页的形式获取信息
     * @param name 以role名称搜索
     * @param pageNum 第几页
     * @param pageSize 页面大小
     * @return role对象列表
     */
    @GetMapping("/page")
    public Map<String,Object> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
//        System.out.println("<----------------获取role全部信息-------------------->");
        Map<String,Object> map = new HashMap<>();
//        Result.success(roleService.page(new Page<>(pageNum, pageSize), Wrappers.<Role>lambdaQuery().like(Role::getName, name)));
        Page<Role> rolesByPages = roleService.getRolesByPages(name, pageNum, pageSize);
        map.put("code",200);
        map.put("data",rolesByPages);
        return map;
    }

    @PostMapping
    public Map<String,Object> saveRole(@RequestBody String updatedata){
        JSONObject parse = (JSONObject)JSONObject.parse(updatedata);
        Role role = JSONObject.toJavaObject(parse, Role.class);
//        System.out.println("role" + role);

//        System.out.println(role);
        roleService.saveRole(role);
//        System.out.println(role);

        rolePermissionService.saveRolePermissionBatch(role);


        permissionRefresh.setPermisiion();
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","Role添加成功");


        return map;
    }

    @GetMapping
    public Map<String,Object> getAllRole(){
        Map<String,Object> map = new HashMap<>();

        List<Role> allRoles = roleService.getAllRoles();

        map.put("code",200);
        map.put("data",allRoles);
        return map;
    }


    @ResponseBody
    @PutMapping
    public Map<String,Object> updateRole(@RequestBody String updatedata){
        Map<String, Object> map = new HashMap<>();

        //返回的role对象中未存在的删除
        JSONObject parse = (JSONObject)JSONObject.parse(updatedata);
    
        JSONArray jsonArray = (JSONArray)parse.get("a");
        JSONObject jsonObject = (JSONObject)parse.get("b");
//        System.out.println("传回来的数据："+parse.toJSONString());

        Role prerole = null;
        Role nowrole = JSONObject.toJavaObject(jsonObject,Role.class);
        for (Object o : jsonArray) {
            if(o instanceof String){
                continue;
            }
            JSONObject temp = (JSONObject)o;
            if(temp.get("id") == nowrole.getId()){
//                if(temp)
                prerole = JSONObject.toJavaObject(temp,Role.class);
            }
        }
//        System.out.println("原始对象：" + prerole);
//        System.out.println("当前对象：" + nowrole);


        rolePermissionService.updatePermissionBatch(prerole,nowrole);
        mybatisPlusRedisCache.clear("server.main.mapper.RoleMapper");
//        MybatisPlusRedisCache cache = new MybatisPlusRedisCache("server.main.mapper.RoleMapper");
//        cache.clear();
//        mybatisPlusRedisCache.removeObject("server.main.mapper.RoleMapper");
//        RedisTemplate bean = SpringBeanUtils.getBean(RedisTemplate.class);

//        bean.delete()

        permissionRefresh.setPermisiion();
        map.put("code",200);
        map.put("msg","更新成功");

        return map;
    }

    @DeleteMapping
    public Map<String,Object> dodelete(@RequestParam String deleteid){
        Map<String,Object> map = new HashMap<>();
        roleService.deleteRoleById(deleteid);
        map.put("code",200);
        map.put("msg","删除成功");
        permissionRefresh.setPermisiion();
        return map;
    }


    @PostMapping("/updaterole")
    public Map<String,Object> updaterole(@RequestBody String rolestring){
//        System.out.println("___________________________________________");
        Map<String,Object> map = new HashMap<>();
        JSONObject parse = (JSONObject)JSONObject.parse(rolestring);
        Role role = JSONObject.toJavaObject(parse, Role.class);
//        System.out.println(role);
        roleService.save(role);
//        roleService.saveRole(role);




        map.put("code",200);
        map.put("msg","更新成功");

        return map;
    }



}
